﻿namespace TheCollectiveNetwork.Web
{
    using System.Security.Authentication;
    using System.Security.Principal;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using System.ServiceModel.DomainServices.Server.ApplicationServices;
    using System.Threading;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Security.Cryptography;
    using System.Text.RegularExpressions;
    using TheCollectiveNetwork.Classes.Security;
    using TheCollectiveNetwork.Classes.Debugging;
    
    /// <summary>
    /// RIA Services DomainService responsible for authenticating users when
    /// they try to log on to the application.
    ///
    /// Most of the functionality is already provided by the base class
    /// AuthenticationBase
    ///
    /// Service URL: /Services/TheCollectiveNetwork-Web-AuthenticationService.svc
    /// </summary>
    [EnableClientAccess()]    
    public class AuthenticationService : AuthenticationBase<User> {
              
        // Checks that the username and password are valid using the custom role provider        
        protected override bool ValidateUser(string p_username, string p_password)
        {
           
            TCNMembershipProvider tcnmember = new TCNMembershipProvider();
            return tcnmember.ValidateUser(p_username, p_password);
           
        }

        // Loads the roles for the user using the custom security class
        [RequiresAuthentication()]
        protected override User GetAuthenticatedUser(IPrincipal principal)
        {
            User user = new User();
            TCNMembershipProvider tcnmember = new TCNMembershipProvider();
            TCNRoleProvider tcnrp = new TCNRoleProvider();           

            user.Name = principal.Identity.Name;                               
            user.Roles = tcnrp.GetRolesForUser(user.Name);
            user.RequiresPasswordChange = tcnmember.RequiresPasswordChange(principal.Identity.Name);
            
            return user;
             
        }

        
            
    }
}
